function MovementAdd_Code(id) {
	var date=new Date();
	var hidden1="";
	var hidden2="hidden";
	if(id!=0)
	{
		var hidden1="hidden";
		var hidden2="";
	}
	var buffer = new StringBuffer();
	buffer.append('<div id="movementid" class="crmsheet border-bottom" val="'+id+'"><div class="ctitle"><div class="cname">调拨单号</div><div class="require">*</div></div><div class="ctext"><input id="txt_num" type="text" placeholder="" readonly="readonly" value=""/></div><div class="carrow"></div></div>');
	buffer.append('<div id="supplier" class="crmsheet border-bottom"><div class="ctitle"><div class="cname">调出仓库</div><div class="require">*</div></div><div class="ctext"><select id="txt_fromlibraryid"></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">调入仓库</div><div class="require">*</div></div><div class="ctext"><select id="txt_tolibraryid"></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom date"><div class="ctitle"><div class="cname">调拨日期</div><div class="require">*</div></div><div class="ctext"><input id="txt_movedate" type="text" placeholder="选择调拨日期" readonly="readonly" value="'+date.Format("yyyy-MM-dd")+'"/></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom" style="height:120px;"><div class="ctitle"><div class="cname">备注</div></div><textarea id="txt_bak" placeholder="填写备注" onfocus="ScrollTop()"></textarea></div>');
	buffer.append('<div id="product_tool" class="pline border-bottom"><font style="padding-left:10px;" class="'+hidden1+'">选择产品</font><table class="'+hidden2+'"><tr><td width="40%" >产品名称</td><td width="10%">单位</td><td width="18%">价格</td><td width="10%">数量</td><td width="22%">总金额</td></tr></table></div>');
	buffer.append('<div id="product_panel"><table id="porderdetail"></table></div>');
	buffer.append('<div id="add_product" class="add_product"><img src="img/add.png"/></div>');
	buffer.append('<div id="candelete" class="loading hidden" style="height:14px; margin-top:10px; margin-bottom:30px; float:left;">长按产品名称可以删除哦</div>');
	return buffer.toString();
};

var map;
var wsub;
var count=0;
var myScroll3;
function Init_MovementAdd(){
	var movementid=$("#movementid").attr("val");
	var customer;
	if(movementid!=0)
	{
		var data = {};
		data.act = "One_Movement";
		data.id=movementid;
		data.source=source;
		data.userid=userid;
		data.comid=comid;
		data.sign=GetSign(data.act);
		mui.getJSON(crm_domain, data, function (json) {
	            if (json.state) {
	                movement=json.data.movement;
	                $("#txt_num").val(movement.Num);
	                $("#txt_movedate").val(movement.MoveDate.ToDate());
	                $("#txt_bak").val(movement.Bak);
	                Init_MLibrary(movement.FromLibraryID,movement.ToLibraryID);
	                Init_MovementProduct(json.data.movementdetail);
	            } else {
					plus.nativeUI.toast(json.msg);
	            }
	    });
    }
	else{
		var data = {};
		data.act = "Table_Num";
		data.flag="movement";
		data.comid=comid;
		data.sign=GetSign(data.act);
		mui.getJSON(crm_domain, data, function (json) {
	            if (json.state) {
	                $("#txt_num").val(json.data);
	            }
	    });
	    Init_MLibrary(0,0);
	}

	
	get("add_product").addEventListener("tap",function(){
	    	myScroll3 = OpenScroll2(4,"选择产品","",function(pageindex){
										Receive_ProductSelect(pageindex);
								},function(){
									
								},ProductSelect_Code("movement"),70);
								Receive_ProductSelect(1);
								get("find2").addEventListener("tap",function(){
				      				$("#searchicon2").hide();
	      							$("#searchicons2").show();
	      							$("#keyword2").attr("placeholder","产品名称，编号");
			    					if($("keyword2").val()!="")
			    					{
			    						//WorkMate_Search();
			    					}
				      			});
	    });
	mui("#contentlist2").on("tap",".date",function(){
			obj10=this;
			var t=new Date();
			if($(this).find("input").val()!="")
			{
				var year_month_day=$(this).find("input").val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]);
			}
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$(obj10).find("input").val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				console.log( "未选择日期："+e.message );
			},{date:t});
		});
	
};

function Init_MLibrary(fromlibraryid,tolibraryid){
	var data = {};
		data.act = "Init_Library_Dialog";
		data.source=source;
		data.userid=userid;
		data.comid=comid;
		data.sign=GetSign(data.act);
		mui.getJSON(crm_domain, data, function (json) {
			
	            if (json.state) {
	            	var str1="",str2="";
	            	var selected1="",selected2="";
	            	$.each(json.data, function(i,info) {
	            		if(info.ID==fromlibraryid)
	            		{
	            			selected1="selected='selected'";
	            		}else
	            		{
	            			selected1="";
	            		}
	            		str1+='<option value="'+info.ID+'" '+selected1+'>'+info.Name+'</option>';
	            		
	            		if(info.ID==tolibraryid)
	            		{
	            			selected2="selected='selected'";
	            		}else
	            		{
	            			selected2="";
	            		}
	            		str2+='<option value="'+info.ID+'" '+selected2+'>'+info.Name+'</option>';
	            	});
	            	$("#txt_fromlibraryid").html(str1);
	            	$("#txt_tolibraryid").html(str2);
	            }
	    });
};

function Init_MovementProduct(orderdetail){
	$.each(orderdetail,function(i,info){
		$("#product_panel table").append('<tr id="'+info.ProductID+'"><td width="40%" >'+info.ProductName+'</td><td width="10%">'+info.Unit+'</td><td width="18%">'+info.CostPrice+'</td><td width="10%"><div style="width:40px; height:100%; margin:0 auto;" class="ui-border-radius"><input type="text" value="'+info.Count+'" onkeyup="Check_Money(this);Cal_Movement(this)" onfocus="ScrollTop()"/></div></td><td width="22%">'+info.AllMoney+'</td></tr>');
	});
};


function Save_Movement() {
    var data = {};
        data.act = "Save_Movement"; 
        data.userid=userid;
		data.comid=comid;
        data.id = $("#movementid").attr("val");
        data.num = $("#txt_num").val();
        data.fromlibraryid = $("#txt_fromlibraryid").val();
        data.tolibraryid = $("#txt_tolibraryid").val();
        data.movedate = $("#txt_movedate").val();
        data.bak = $("#txt_bak").val();
        data.source = source;
        data.version = version;
        data.count = 0;
        data.allmoney=0;
        var movementdetail = new Array();
        var error = false;
        if (data.fromlibraryid == data.tolibraryid) {
            plus.nativeUI.toast("调出仓库和调入仓库不能相同");
            return;
        }
        if (data.movedate.Trim()== "") {
            plus.nativeUI.toast("请选择调拨日期");
            return;
        }
        $("#product_panel tr").each(function (i, row) {
            var detail = "";
                var input = $(row);
                detail += input.attr("id") + "_"; //拼接产品id
                input = $(row).find("td:eq(1)");
                detail += input.html() + "_"; //计量单位
                input = $(row).find("td:eq(2)");
                detail += input.html() + "_"; //拼接价格
                var costprice=parseFloat(input.html());
                input = $(row).find("td:eq(3) input");
                detail += input.val() + "_"; //拼接数量
                var count = parseInt(input.val());
                if (input.val().Trim()==""  || input.val().Trim()=="0") {
                    plus.nativeUI.toast("请填写数量");
                    input.focus();
                    error = true;
                    return true;
                }
                detail += (costprice * count).toFixed(2); //拼接总金额
                data.count += count;
                data.allmoney += costprice * count;
                movementdetail.push(detail);
        });
        if (error) {
            return false;
        }
        
        data.movementdetail = movementdetail + "";
        if(data.movementdetail=="")
        {
        	plus.nativeUI.toast("请选择产品");
        	return;
        }
        data.sign=GetSign(data.act);
		plus.nativeUI.showWaiting( "正在保存..." );
        
		mui.getJSON(crm_domain, data, function (json) {
			plus.nativeUI.closeWaiting();
			plus.nativeUI.toast(json.msg);
            if (json.state) {
                CloseWindow();
                if(data.id==0)
                {
                	Clear_Form();
                	Movement_Search();
                }else{
                	Init_MovementDetail();
                	obj.find(".orderprice").html("总额："+data.allmoney.toFixed(2));
                }
            } 
        });
};


//计算调拨单价格
function Cal_Movement(o) {
    var tr = $(o).parent().parent().parent();
    var costprice = tr.find("td:eq(2)").html();
    var count = tr.find("td:eq(3)").find("input").val();
    tr.find("td:eq(4)").html((parseFloat(costprice * count)).toFixed(2));

};